<template>
	<view id="page_info">
		<!-- <inputModal :show="show" @alert="cancelModal" @confiem="confiemFunc"></inputModal> -->
		<view class="op_info">
			<view class="op_icon">
				<u-icon size="120" label-size="50" label-color="rgba(255, 255, 255, 1)" margin-left="40"
					color="rgba(255, 255, 255, 1)" :label="pageid==1?'待签字':(pageid==2?'已签字':(pageid==3?'拒绝交易':'交易成功'))"
					:name="pageid==1?'heart':(pageid==2?'eye':(pageid==3?'email':'download'))"></u-icon>
			</view>
			<view>
				<view class="list_info">
					<view class="list_top"></view>
					<view class="order_card" v-for="(orderitem) in order_list"
						:key="orderitem.id">
						<view class="choose_icon">
							<u-icon v-if="showpopup  && pageid >1" name="checkmark-circle" size="46" margin-left="20"
								:style="{'color' :(chooseall?'rgba(255, 87, 51, 1)':'rgba(112, 112, 112, 1)')}">
							</u-icon>
						</view>
						<view class="goods_image">
							<image :src="orderitem.productCover">
							</image>
						</view>
						<view class="goods_text">
							<view class="goods_title">
								<view class="goods_name">{{orderitem.productSpecies}}</view>
							</view>
							<view class="goods_details">
								<view>品种：{{orderitem.productSpecies}}</view>
								<view>重量：{{orderitem.orderWeight}}kg</view>
								<view>下单日期：{{orderitem.createTime}}</view>
							</view>
							<view class="button_box" >
								<!-- <u-modal v-model="show" @confirm="confirm" @cancel="cancel" :content="content" :show-cancel-button = "true"></u-modal> -->
								<button class="publish_button" v-if="pageid==1" @click="open(orderitem.id)">拒绝</button>
								<button class="publish_button1" v-if="pageid==1" @click="placeorder(orderitem.id)">签字</button>
								<button class="publish_button2" v-if="pageid==2||pageid==4" @click="loadPdf(orderitem.id)">合同</button>
								<button class="publish_button2" v-if="pageid>1"@click="deleteMineOrder(orderitem.id)">删除</button>
							</view>
						</view>
					</view>
					<view class="bottom_box" v-if="showpopup && pageid >1">
						<view class="allchoose_icon">
							<u-icon name="checkmark-circle" label="全选" size="46" margin-left="20" @click="allchoose()"
								:style="{'color' :(chooseall?'rgba(255, 87, 51, 1)':'rgba(112, 112, 112, 1)')}">
							</u-icon>
						</view>
						<view class="bottom_button" v-if="pageid>1">
							<button class="publish_button2"
								@click="goto('../../subpages/Details/product_details')">删除</button>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-back-top :scroll-top="scrollTop" mode="square" tips="顶部"></u-back-top>
		<!-- 签字弹窗 -->
		<sign v-model="showSign" @getsign="getSign"></sign>
	</view>
</template>
<script>
	import sign from 'components/sign/signwx.vue';
	import inputModal from "components/modals/inputModal.vue"
	import {openPdf} from "utils/file"
	import {
		getOrderPageByType,
		agreeOrderById,
		refuseOrderById,
		delOrderByIdMine,
		getOrderContractInfoById
	} from 'apis/farmer/order.js'
	import {
		pathToBase64
	} from 'utils/image.js'
	export default {
		components: {
			sign,
			inputModal
		},
		data() {
			return {
				//页面状态
				pageid: 1,
				//点击事件参数
				chooseall: false,
				showpopup: false,
				showSign: false,
				scrollTop: 0,
				orderId: '',
				pageNo: 1,
				pageSize: 10,
				order_list: [
					// {
					// 	createTime:"2022.02.04",
					// 	id: "12312312312",
					// 	orderStatus: 1,
					// 	orderWeight:"10",
					// 	productCover: "http://82.157.157.133:9000/img/picture/43.jpg",
					// 	productId: "4122414",
					// 	productSpecies:"苹果",
					// 	productVariety: "红富士",
					// 	type: 0,
					// 	unitProce: 10
					// }
				],
			}
		},
		methods: {
			async loadPdf(id){
				const {code,data} = await getOrderContractInfoById(id)
				if(code === 200 && data) return openPdf(data)
				this.$errorMsg("合同暂未生成")
			},
			// 拒绝订单时的弹窗
			async open(id) {
				const inputContent = await this.$showInputModule("请输入拒绝理由")
				const {code,msg} = await refuseOrderById(inputContent, id)
				if(code === 200) return this.$successMsg("拒绝成功")
				this.removeData(id)
				return this.$errorMsg(msg)
			},
			removeData(id){
				order_list = order_list.filter(item => item.id !== id)
			},

			async getSign(sellerSignature) { //得到签名后提交订单
				const {
					orderId
				} = this
				this.showSign = false
				const signdata = await pathToBase64(sellerSignature)
				const {code,msg} = await agreeOrderById(orderId, signdata)
				if(code === 200){
					this.removeData(id)
					this.$successMsg("success")
				}else{
					this.$errorMsg(msg)
				} 
			},

			deleteMineOrder(id){
				let type = this.pageid
				delOrderByIdMine(type,id)
			},

			async getData() {
				let type = this.pageid
				const {
					pageNo,
					pageSize
				} = this
				let result = await getOrderPageByType(pageNo, pageSize, type)
				if (result.code === 200) {
					// console.log(result)
					this.order_list = [...this.order_list, ...result.data.list]
					this.pageNo++
				}
			},
			allchoose() { //点击选择
				this.chooseall = !this.chooseall;
			},
			show_popup() {
				this.showpopup = !this.showpopup;
			},
			placeorder(id) { //验证规则
				this.orderId = id;
				this.showSign = !this.showSign;
			},
		},
		onLoad(option) {
			if (option.pageid) {
				this.pageid = option.pageid;
			}
			this.getData()
		},
		onReachBottom() { //到达底部
			this.getData();
		},

		onPageScroll(e) { //生命周期函数
			this.scrollTop = e.scrollTop
		}
	}
</script>
<style>
	@import url("orderlist.css");
</style>
